-- OldStrongbox
-- Created by chengb Dec/31/2015
-- 古老的保险箱格子

require "game/ui/form/dungeon/special/UIStrongBox"

-- 初始化
function initOldStrongbox(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 判断能否拾取
            if DungeonBossM.isInBossLayer() and not DungeonBossM.canPickUpTarget() then
                alert(getLocStr("pick_up_instance_target_fail"));
                return;
            end

            local function okFunc()
                -- 执行奖励
                local pos = self.gridData:getPos();
                DungeonActionM.go("old_strongbox_bonus", pos);
            end

            UIMgr.getCurrentScene():removeFormByName("UIStrongBox");
            local uiBuildingView = UIStrongBox.create(self.gridData, okFunc, 0, buildingInfo["dialog"]);
            UIMgr.getCurrentScene():addForm(uiBuildingView);
            AudioM.playFx("market_open");
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    if self.gridData.need_prison == 1 then
        -- 初始时需要监狱图标的遮罩
        local prisonIcon = getLevelItemIconPath("prison");
        local prisonImg  = ccui.ImageView:create(prisonIcon);
        prisonImg:setName("prison_icon");

        self.item:addChild(prisonImg);
    end
end
